package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ConsultRecordDTO;
import com.api.shopapi.service.ConsultRecordService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * 咨询回复
 * */

@RestController
@RequestMapping("ConsultRecord")
public class ConsultRecordController extends BaseController{

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ConsultRecordServiceImplOne")
    private ConsultRecordService consultRecordService;

    @RequestMapping(value = "AppAdd",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String question = jsonObject.getString("question");
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ConsultRecord/AppAdd");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ConsultRecord consultRecord=new ConsultRecord();
            consultRecord.setConsultRecordCode(UUID.randomUUID().toString());
            consultRecord.setUserCode(userCode);
            consultRecord.setQuestion(question);
            consultRecord.setQuestionTime(new Date());
            consultRecord.setStatus(1);
            User user = userService.getByUserCode(userCode);
            consultRecord.setGovernmentCode(user.getGovernmentCode());
            consultRecordService.insert(consultRecord);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ConsultRecord/AppAdd", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "StaffAdd",method = RequestMethod.POST)
    public MyResult StaffAdd(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String question = jsonObject.getString("question");
            String staffCode = jsonObject.getString("staffCode");
            String reply = jsonObject.getString("Reply");
            int status = jsonObject.getIntValue("status");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ConsultRecord/StaffAdd");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ConsultRecord consultRecord=new ConsultRecord();
            consultRecord.setConsultRecordCode(UUID.randomUUID().toString());
            consultRecord.setUserCode(userCode);
            consultRecord.setQuestion(question);
            consultRecord.setQuestionTime(new Date());
            consultRecord.setStatus(status);
            User user = userService.getByUserCode(userCode);
            consultRecord.setGovernmentCode(user.getGovernmentCode());
            if (reply!=null&&!reply.equals("")){
                consultRecord.setStaffCode(staffCode);
                consultRecord.setReply(reply);
                consultRecord.setReplyTime(new Date());
            }
            consultRecordService.insert(consultRecord);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ConsultRecord/StaffAdd", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Reply",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String reply = jsonObject.getString("reply");
            String consultRecordCode = jsonObject.getString("consultRecordCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ConsultRecord/Reply");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ConsultRecord consultRecord = consultRecordService.getByConsultRecordCode(consultRecordCode);
            consultRecord.setStatus(2);
            consultRecord.setStaffCode(staffCode);
            consultRecord.setReplyTime(new Date());
            consultRecord.setReply(reply);
            consultRecordService.update(consultRecord);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ConsultRecord/Reply", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "PageApp",method = RequestMethod.POST)
    public MyResult PageApp(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            int status = jsonObject.getIntValue("status");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ConsultRecord/PageApp");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<ConsultRecordDTO> consultRecordDTOPageInfo = consultRecordService.getPageApp(pageIndex, pageSize, status, userCode);
            for (ConsultRecordDTO consultRecordDTO : consultRecordDTOPageInfo.getList()) {
                if (consultRecordDTO.getStaffCode()!=null&&!consultRecordDTO.getStaffCode().equals("")){
                    Staff staff = staffService.getByStaffCode(consultRecordDTO.getStaffCode());
                    consultRecordDTO.setStaffName(staff.getRealName());
                    consultRecordDTO.setStaffPhone(staff.getPhone());
                }
                User user = userService.getByUserCode(userCode);
                consultRecordDTO.setUserName(user.getName());
                consultRecordDTO.setUserPhone(user.getPhone());
            }

            return success(consultRecordDTOPageInfo);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ConsultRecord/PageApp", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "PageAdm",method = RequestMethod.POST)
    public MyResult PageAdm(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int status = jsonObject.getIntValue("status");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime=endTime+" 23:59:59";
            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ConsultRecord/PageAdm");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff = staffService.getByStaffCode(staffCode);
            PageInfo<ConsultRecordDTO> consultRecordDTOPageInfo =
                    consultRecordService.getPageAdm(pageIndex, pageSize,status,startTime,endTime,staff.getGovernmentCode());
            for (ConsultRecordDTO consultRecordDTO : consultRecordDTOPageInfo.getList()) {
                if (consultRecordDTO.getStaffCode()!=null&&!consultRecordDTO.getStaffCode().equals("")){
                    consultRecordDTO.setStaffName(staff.getRealName());
                    consultRecordDTO.setStaffPhone(staff.getPhone());
                }
                User user = userService.getByUserCode(consultRecordDTO.getUserCode());
                consultRecordDTO.setUserName(user.getName());
                consultRecordDTO.setUserPhone(user.getPhone());
            }
            return success(consultRecordDTOPageInfo);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ConsultRecord/PageAdm", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "PageToGovPlatform",method = RequestMethod.POST)
    public MyResult PageToGovPlatform(@RequestBody JSONObject jsonObject){
        try {

            int status = jsonObject.getIntValue("status");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            String governmentCode = jsonObject.getString("governmentCode");
            endTime=endTime+" 23:59:59";


            PageInfo<ConsultRecordDTO> consultRecordDTOPageInfo =
                    consultRecordService.getPageAdm(pageIndex, pageSize,status,startTime,endTime,governmentCode);
            for (ConsultRecordDTO consultRecordDTO : consultRecordDTOPageInfo.getList()) {
                if (consultRecordDTO.getStaffCode()!=null&&!consultRecordDTO.getStaffCode().equals("")){
                    Staff staff = staffService.getByStaffCode(consultRecordDTO.getStaffCode());
                    consultRecordDTO.setStaffName(staff.getRealName());
                    consultRecordDTO.setStaffPhone(staff.getPhone());
                }
                User user = userService.getByUserCode(consultRecordDTO.getUserCode());
                consultRecordDTO.setUserName(user.getName());
                consultRecordDTO.setUserPhone(user.getPhone());
            }
            return success(consultRecordDTOPageInfo);
        } catch (Exception e) {
//            e.printStackTrace();
            insertError("/ConsultRecord/PageToGovPlatform", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
